package service

import (
	"awesomeTask/application/client"
	"awesomeTask/application/cron"
	"awesomeTask/application/models/master"
	"awesomeTask/application/timer"
	"awesomeTask/application/variable"
	"awesomeTask/system/echo"
	"awesomeTask/system/event"
	"awesomeTask/system/log"
)

type TimeService struct {
}

/**
注册服务器的定时任务
*/
func (ioc TimeService) Register() {

	timer.SetTimeInterval(func() {
		master.GetMasterObj().RegisterMaster()
		if client.ServerCon != nil {
			variable.VoteIsFinish = true
			echo.Yellow(client.ServerCon.RemoteAddr().String() + "serverCon==========>")
		} else {
			if variable.IsMaster == false {
				cron.RemoveAllTask()
			}
		}
	}, variable.RegisterTick)
	timer.SetTimeInterval(func() {
		if client.ServerCon == nil {
			master.GetMasterObj().FindMaster()
		} else {
			log.GetLogger().Info(client.ServerCon.RemoteAddr().String())
		}
	}, 60)

	timer.SetTimeInterval(func() {
		event.Dispatch(variable.ON_TRY_KEEPALIVE)
	}, 60)
}
func (ioc TimeService) Boot() {

}
